/**
 * 触觉反馈Hook
 * Story 5: Task 8 - 触觉反馈集成
 */

export type HapticFeedbackType = 'light' | 'medium' | 'heavy' | 'success' | 'error'

interface HapticFeedbackOptions {
  enabled?: boolean
}

/**
 * 触觉反馈Hook
 * 提供跨平台的触觉反馈功能（iOS Taptic Engine / Android Vibration API）
 */
export const useHapticFeedback = (options: HapticFeedbackOptions = {}) => {
  const { enabled = true } = options

  /**
   * 触发触觉反馈
   */
  const trigger = (type: HapticFeedbackType = 'medium') => {
    if (!enabled || typeof navigator === 'undefined' || !navigator.vibrate) {
      return
    }

    const patterns: Record<HapticFeedbackType, number | number[]> = {
      light: 10,
      medium: 30,
      heavy: 50,
      success: [50, 30, 50],
      error: [30, 50, 30, 50],
    }

    try {
      const pattern = patterns[type]
      navigator.vibrate(pattern)
    } catch (error) {
      console.warn('Haptic feedback not supported:', error)
    }
  }

  return {
    trigger,
    isSupported: typeof navigator !== 'undefined' && 'vibrate' in navigator,
  }
}

/**
 * 触觉反馈工具函数
 */
export const hapticFeedback = {
  light: () => navigator.vibrate?.(10),
  medium: () => navigator.vibrate?.(30),
  heavy: () => navigator.vibrate?.(50),
  success: () => navigator.vibrate?.([50, 30, 50]),
  error: () => navigator.vibrate?.([30, 50, 30, 50]),
}
